package com.ss;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.esotericsoftware.reflectasm.ConstructorAccess;
import org.springframework.beans.BeanUtils;

/**
 * @author Ss
 * @version V1.0
 * @description
 * @date 2019/5/9
 */


public class MainTest {

    private static Person p;
    private static int num = 100000;

    static {
        p = new Person();
        p.setName("XXX");
        p.setCourse("EEEE");
        p.setAge(99);
        p.setNickName("Tom");
    }

    public static void main(String[] args) {


        proCopyTest();
    }


    // 创建对象 测试
    public static void use_reflectasm(){
        ConstructorAccess<Student> access = ConstructorAccess.get(Student.class); //放在循环外面 相当于从缓存中获取
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            Student stu = access.newInstance();
        }
        System.out.printf("took time: %d(ms)%n",System.currentTimeMillis() - start);
    }

    public static void proCopyTest() {

        System.out.println("对象属性转换 运行次数：" + num);

        System.out.println("原始对象： " + p);
        System.out.println("-----------------------------------");
        Student stu = new Student();
        long s0 = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            Student student = new Student();
            student.setName(p.getName());
            student.setCourse(p.getCourse());
            student.setAge(p.getAge());
            student.setNickname(p.getNickName());
            if (i == 0){
                stu = student;
            }
        }
        System.out.println("手工设置耗时: " + (System.currentTimeMillis() - s0));
        System.out.println("拷贝结果：" + stu);
        System.out.println("-----------------------------------");

        long s = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            Student student = WrappedBeanCopier.copyProperties(p, Student.class);
            if (i == 0){
                stu = student;
            }
        }
        System.out.println("WrappedBeanCopier耗时: " + (System.currentTimeMillis() - s));
        System.out.println("拷贝结果：" + stu);
        System.out.println("-----------------------------------");

        long s2 = System.currentTimeMillis();

        for (int i = 0; i < num; i++) {
            Student student = JSONObject.parseObject(JSON.toJSONString(p), Student.class);
            if (i == 0){
                stu = student;
            }
        }

        System.out.println("FastJson耗时:" + (System.currentTimeMillis() - s2));
        System.out.println("拷贝结果：" + stu);
        System.out.println("-----------------------------------");

        long s3 = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            Student student = new Student();
            BeanUtils.copyProperties(p, student);
            if (i == 0){
                stu = student;
            }
        }
        System.out.println("BeanUtils耗时:" + (System.currentTimeMillis() - s3));
        System.out.println("拷贝结果：" + stu);
        System.out.println("-----------------------------------");

        long s4 = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            Student student = WrappedReflectasmCopier.transferObjectIgnoreCase(p, Student.class);
            if (i == 0){
                stu = student;
            }
        }
        System.out.println("WrapperReflectasm耗时:" + (System.currentTimeMillis() - s4));
        System.out.println("拷贝结果：" + stu);
    }




}
